﻿Imports System.Web.UI.WebControls
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports ExtensionLibrary

''' <summary>
''' Tests the methods within the <see cref="ListControlExtensions">ListControlExtensions</see> class.
''' </summary>
''' <revisions>
'''   <revision date="11/02/2008" author="Chad Green" version="1.0.0.0">Initial Development</revision>
''' </revisions>
<TestClass()> _
Public Class ListControlExtensionsTest

  Private testContextInstance As TestContext

  ''' <summary>
  ''' Gets or sets the test context which provides information about and functionality for the current test run.
  ''' </summary>
  ''' <value>The test context.</value>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.00.000">Initial Development</revision>
  ''' </revisions>
  Public Property TestContext() As TestContext
    Get
      Return testContextInstance
    End Get
    Set(ByVal value As TestContext)
      testContextInstance = value
    End Set
  End Property

#Region " ToArray"

  ''' <summary>
  ''' Tests the <see cref="ListControlExtensions.ToArray">ToArray</see> method using a <see cref="ListBox">ListBox</see>.
  ''' </summary>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.00.000">Initial Development</revision>
  ''' </revisions>
  <TestMethod()> Public Sub ToArrayTest1()

    ' Variable Declarations
    Dim lcInput As ListControl = New ListBox
    Dim strExpected(0) As String
    Dim strActual() As String

    ' Initialize the Input value
    lcInput.AddItem("Test Value 1", "1", True)
    lcInput.AddItem("Test Value 2", "2", False)
    lcInput.AddItem("Test Value 3", "3", True)
    lcInput.AddItem("Test Value 4", "4", False)
    lcInput.AddItem("Test Value 5", "5", True)

    ' Initialize the Expected value
    strExpected(0) = "5"

    ' Perform the test
    strActual = ListControlExtensions.ToArray(lcInput)

    ' Perform the validations
    Assert.AreEqual(strExpected(0), strActual(0))

  End Sub

  ''' <summary>
  ''' Tests the <see cref="ListControlExtensions.ToArray">ToArray</see> method using a <see cref="DropDownList">DropDownList</see>.
  ''' </summary>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.00.000">Initial Development</revision>
  ''' </revisions>
  <TestMethod()> Public Sub ToArrayTest2()

    ' Variable Declarations
    Dim lcInput As ListControl = New DropDownList
    Dim strExpected(0) As String
    Dim strActual() As String

    ' Initialize the Input value
    lcInput.AddItem("Test Value 1", "1", False)
    lcInput.AddItem("Test Value 2", "2", False)
    lcInput.AddItem("Test Value 3", "3", True)
    lcInput.AddItem("Test Value 4", "4", False)
    lcInput.AddItem("Test Value 5", "5", False)

    ' Initialize the Expected value
    strExpected(0) = "3"

    ' Perform the test
    strActual = ListControlExtensions.ToArray(lcInput)

    ' Perform the validations
    Assert.AreEqual(strExpected(0), strActual(0))

  End Sub

#End Region

#Region " SortByValue"

  ''' <summary>
  ''' Tests the <see cref="ListControlExtensions.SortByValue">SortByValue</see> method using a <see cref="ListBox">ListBox</see> control.
  ''' </summary>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.00.000">Initial Development</revision>
  ''' </revisions>
  <TestMethod()> Public Sub SortByValueTest1()

    ' Variable Declarations
    Dim lcExpected As New ListBox
    Dim lcActual As New ListBox

    ' Initialize the Input variable
    lcActual.AddItem("Test Value 5", "5", False)
    lcActual.AddItem("Test Value 4", "4", False)
    lcActual.AddItem("Test Value 3", "3", False)
    lcActual.AddItem("Test Value 2", "2", False)
    lcActual.AddItem("Test Value 1", "1", False)

    ' Initialize the Expected variable
    lcExpected.AddItem("Test Value 1", "1", False)
    lcExpected.AddItem("Test Value 2", "2", False)
    lcExpected.AddItem("Test Value 3", "3", False)
    lcExpected.AddItem("Test Value 4", "4", False)
    lcExpected.AddItem("Test Value 5", "5", False)

    ' Perform the test
    lcActual.SortByValue()

    ' Perform the validations
    For intItem As Integer = 0 To lcExpected.Items.Count - 1
      Assert.AreEqual(lcExpected.Items(intItem).Value, lcActual.Items(intItem).Value)
    Next intItem

  End Sub

  ''' <summary>
  ''' Tests the <see cref="ListControlExtensions.SortByValue">SortByValue</see> method using a <see cref="DropDownList">DropDownList</see> control.
  ''' </summary>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.00.000">Initial Development</revision>
  ''' </revisions>
  <TestMethod()> Public Sub SortByValueTest2()

    ' Variable Declarations
    Dim lcExpected As New DropDownList
    Dim lcActual As New DropDownList

    ' Initialize the Input variable
    lcActual.AddItem("Test Value 5", "5", False)
    lcActual.AddItem("Test Value 4", "4", False)
    lcActual.AddItem("Test Value 3", "3", False)
    lcActual.AddItem("Test Value 2", "2", False)
    lcActual.AddItem("Test Value 1", "1", False)

    ' Initialize the Expected variable
    lcExpected.AddItem("Test Value 1", "1", False)
    lcExpected.AddItem("Test Value 2", "2", False)
    lcExpected.AddItem("Test Value 3", "3", False)
    lcExpected.AddItem("Test Value 4", "4", False)
    lcExpected.AddItem("Test Value 5", "5", False)

    ' Perform the test
    lcActual.SortByValue()

    ' Perform the validations
    For intItem As Integer = 0 To lcExpected.Items.Count - 1
      Assert.AreEqual(lcExpected.Items(intItem).Value, lcActual.Items(intItem).Value)
    Next intItem

  End Sub

#End Region

#Region " SortByText"

  ''' <summary>
  ''' Tests the <see cref="ListControlExtensions.SortByText">SortByText</see> method using a <see cref="ListBox">ListBox</see> control.
  ''' </summary>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.00.000">Initial Development</revision>
  ''' </revisions>
  <TestMethod()> Public Sub SortByTextTest1()

    ' Variable Declarations
    Dim lcExpected As New ListBox
    Dim lcActual As New ListBox

    ' Initialize the Input variable
    lcActual.AddItem("Test Value 5", "5", False)
    lcActual.AddItem("Test Value 4", "4", False)
    lcActual.AddItem("Test Value 3", "3", False)
    lcActual.AddItem("Test Value 2", "2", False)
    lcActual.AddItem("Test Value 1", "1", False)

    ' Initialize the Expected variable
    lcExpected.AddItem("Test Value 1", "1", False)
    lcExpected.AddItem("Test Value 2", "2", False)
    lcExpected.AddItem("Test Value 3", "3", False)
    lcExpected.AddItem("Test Value 4", "4", False)
    lcExpected.AddItem("Test Value 5", "5", False)

    ' Perform the test
    lcActual.SortByText()

    ' Perform the validations
    For intItem As Integer = 0 To lcExpected.Items.Count - 1
      Assert.AreEqual(lcExpected.Items(intItem).Text, lcActual.Items(intItem).Text)
    Next intItem

  End Sub

  ''' <summary>
  ''' Tests the <see cref="ListControlExtensions.SortByText">SortByText</see> method using a <see cref="DropDownList">DropDownList</see> control.
  ''' </summary>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.00.000">Initial Development</revision>
  ''' </revisions>
  <TestMethod()> Public Sub SortByTextTest2()

    ' Variable Declarations
    Dim lcExpected As New DropDownList
    Dim lcActual As New DropDownList

    ' Initialize the Input variable
    lcActual.AddItem("Test Value 5", "5", False)
    lcActual.AddItem("Test Value 4", "4", False)
    lcActual.AddItem("Test Value 3", "3", False)
    lcActual.AddItem("Test Value 2", "2", False)
    lcActual.AddItem("Test Value 1", "1", False)

    ' Initialize the Expected variable
    lcExpected.AddItem("Test Value 1", "1", False)
    lcExpected.AddItem("Test Value 2", "2", False)
    lcExpected.AddItem("Test Value 3", "3", False)
    lcExpected.AddItem("Test Value 4", "4", False)
    lcExpected.AddItem("Test Value 5", "5", False)

    ' Perform the test
    lcActual.SortByText()

    ' Perform the validations
    For intItem As Integer = 0 To lcExpected.Items.Count - 1
      Assert.AreEqual(lcExpected.Items(intItem).Text, lcActual.Items(intItem).Text)
    Next intItem

  End Sub

#End Region

#Region " AddItem"

  ''' <summary>
  ''' Tests the <see cref="ListControlExtensions.AddItem">AddItem</see> method using a <see cref="ListBox">ListBox</see> control.
  ''' </summary>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.0.0.0">Initial Development</revision>
  ''' </revisions>
  <TestMethod()> Public Sub AddItemTest1()

    ' Variable Declarations
    Dim lbxExpected As New ListBox
    Dim lbxActual As New ListBox
    Dim liListItem As ListItem

    ' Initialize the Expected variable
    liListItem = New ListItem("Test Value 1", "1")
    liListItem.Selected = True
    lbxExpected.Items.Add(liListItem)
    liListItem = New ListItem("Test Value 2", "2")
    liListItem.Selected = False
    lbxExpected.Items.Add(liListItem)
    liListItem = New ListItem("Test Value 3", "3")
    liListItem.Selected = True
    lbxExpected.Items.Add(liListItem)
    liListItem = New ListItem("Test Value 4", "4")
    liListItem.Selected = False
    lbxExpected.Items.Add(liListItem)
    liListItem = New ListItem("Test Value 5", "5")
    liListItem.Selected = True
    lbxExpected.Items.Add(liListItem)

    ' Perform the test
    lbxActual.AddItem("Test Value 1", "1", True)
    lbxActual.AddItem("Test Value 2", "2", False)
    lbxActual.AddItem("Test Value 3", "3", True)
    lbxActual.AddItem("Test Value 4", "4", False)
    lbxActual.AddItem("Test Value 5", "5", True)

    ' Perform the validations
    For intItem As Integer = 0 To lbxExpected.Items.Count - 1
      Assert.AreEqual(lbxExpected.Items(intItem).Value, lbxActual.Items(intItem).Value)
    Next intItem

  End Sub

  ''' <summary>
  ''' Tests the <see cref="ListControlExtensions.AddItem">AddItem</see> method using a <see cref="DropDownList">DropDownList</see> control.
  ''' </summary>
  ''' <revisions>
  '''   <revision date="11/02/2008" author="Chad Green" version="1.0.0.0">Initial Development</revision>
  ''' </revisions>
  <TestMethod()> Public Sub AddItemTest2()

    ' Variable Declarations
    Dim ddlExpected As New DropDownList
    Dim ddlActual As New DropDownList
    Dim liListItem As ListItem

    ' Initialize the Expected variable
    liListItem = New ListItem("Test Value 1", "1")
    liListItem.Selected = False
    ddlExpected.Items.Add(liListItem)
    liListItem = New ListItem("Test Value 2", "2")
    liListItem.Selected = False
    ddlExpected.Items.Add(liListItem)
    liListItem = New ListItem("Test Value 3", "3")
    liListItem.Selected = True
    ddlExpected.Items.Add(liListItem)
    liListItem = New ListItem("Test Value 4", "4")
    liListItem.Selected = False
    ddlExpected.Items.Add(liListItem)
    liListItem = New ListItem("Test Value 5", "5")
    liListItem.Selected = False
    ddlExpected.Items.Add(liListItem)

    ' Perform the test
    ddlActual.AddItem("Test Value 1", "1", False)
    ddlActual.AddItem("Test Value 2", "2", False)
    ddlActual.AddItem("Test Value 3", "3", True)
    ddlActual.AddItem("Test Value 4", "4", False)
    ddlActual.AddItem("Test Value 5", "5", False)

    ' Perform the validations
    For intItem As Integer = 0 To ddlExpected.Items.Count - 1
      Assert.AreEqual(ddlExpected.Items(intItem).Value, ddlActual.Items(intItem).Value)
    Next intItem

  End Sub

#End Region

End Class